package com.jpcc.vw.onlineRadio.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.RemoteException;
import android.text.TextUtils;
/*Start by menglingqiang*/
import android.util.Log;
/*End by menglingqiang*/

import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cns.android.atmospherelamp.AtmosphereLampManager;
import com.jpcc.vw.onlineRadio.R;
import com.jpcc.vw.onlineRadio.adapter.TopRanksListAlbumAdapter;
/*Start by menglingqiang*/
import com.jpcc.vw.onlineRadio.application.MusicApplication;
/*End by menglingqiang*/
import com.jpcc.vw.onlineRadio.base.BaseActivity;
import com.jpcc.vw.onlineRadio.constants.Constants;
import com.jpcc.vw.onlineRadio.model.IRadio;
import com.jpcc.vw.onlineRadio.model.bean.BaseBean;
import com.jpcc.vw.onlineRadio.model.bean.TopListAlbumBean;
import com.jpcc.vw.onlineRadio.model.bean.TopListAlbumData;
import com.jpcc.vw.onlineRadio.model.impl.RadioImpl;
import com.jpcc.vw.onlineRadio.utils.CommonSubscriber;
import com.jpcc.vw.onlineRadio.utils.ToastUtils;
import com.orhanobut.logger.Logger;
/*Start by menglingqiang*/
import com.jpcc.vw.tracelog.TracelogInterface;
/*End by menglingqiang*/
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
/*Start by menglingqiang*/
import java.util.Date;
/*End by menglingqiang*/
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action0;

public class TopListAlbumsActivity extends BaseActivity {


    private IRadio radioModel;
    private boolean is_refresh = true;
    private int page_index = 1;
    private List<TopListAlbumData> datas = new ArrayList<>();
    private TopRanksListAlbumAdapter adapter;
    private long RankId = -1;
    private String key_rank_name="";

    @BindView(R.id.swipe_refrsh_ranks)
    SmartRefreshLayout swipe_refrsh_ranks;
    @BindView(R.id.recyclerview_ranks)
    ListView recyclerview_ranks;
    @BindView(R.id.layout_back)
    RelativeLayout layout_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_show_empty)
    TextView tv_show_empty;
    @BindView(R.id.layout_search)
    RelativeLayout layout_search;
    @BindView(R.id.imv_top_bottom_line)
    ImageView imv_top_bottom_line;
    @BindView(R.id.progress_circular)
    ProgressBar progress_circular;

    @Override
    public int getLayoutId() {
        return R.layout.activity_toplist_ranks;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        layout_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TopListAlbumsActivity.this.finish();
            }
        });
        radioModel = new RadioImpl();
        RankId = getIntent().getLongExtra("key_rank_id", -1);
        if (RankId == -1) {
            this.finish();
        }
        key_rank_name=getIntent().getStringExtra("key_rank_name");
        tv_title.setText(key_rank_name);
        radioModel = new RadioImpl();
        swipe_refrsh_ranks.setEnableRefresh(false);
        swipe_refrsh_ranks.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                is_refresh = true;
                swipe_refrsh_ranks.setEnableLoadmore(true);
                requestAlbumsById();
            }
        });
        swipe_refrsh_ranks.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                is_refresh = false;
                requestAlbumsById();
            }
        });
        layout_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(TopListAlbumsActivity.this,SearchActivity.class);
                startActivity(intent);
            }
        });
        adapter = new TopRanksListAlbumAdapter(this, datas);
        recyclerview_ranks.setAdapter(adapter);
//        ViewGroup parentView = (ViewGroup) recyclerview_ranks.getParent();
//        parentView.addView(emptyView, 2); // 你需要在这儿设置正确的位置，以达到你需要的效果。
//        recyclerview_ranks.setEmptyView(tv_show_empty);
        is_refresh = true;
        requestAlbumsById();
        recyclerview_ranks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(TopListAlbumsActivity.this, AlbumDetailActivity.class);
                intent.putExtra("key_album_id",datas.get(position).getId());
                intent.putExtra("key_is_radio",false);
                intent.putExtra("key_from",4);
                startActivity(intent);
            }
        });
    }



    @Override
    public void doAtmosphere(boolean rgbOn, String color) {
        Logger.i("doAtmosphere" + "rgbOn-->" + rgbOn);
        Logger.i("doAtmosphere" + "color-->" + color);
        if (rgbOn) {
            if (TextUtils.isEmpty(color)) {
                imv_top_bottom_line.setColorFilter(Color.TRANSPARENT);
            } else {
                imv_top_bottom_line.setColorFilter(Color.parseColor(color));
            }
        } else {
            imv_top_bottom_line.setColorFilter(Color.TRANSPARENT);
        }
    }

    @Override
    public void setupData() {
        if (Constants.isRelease) {
            try {
                boolean atmosphereStatus = AtmosphereLampManager.getInstance(this).isHmiAtmosphereLampOn();
                String color = AtmosphereLampManager.getInstance(this).getHmiAtmosphereLampColor();
                doAtmosphere(atmosphereStatus, color);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    private void requestAlbumsById() {
        int local_page = page_index;
        if (is_refresh) {
            local_page = 1;
        }
        /*Start by menglingqiang*/
        Date dt = new Date();
        try {
            TracelogInterface inter = MusicApplication.getLogInterface();
            if(inter != null){
                inter.sendLogData(dt.toString(),dt.toString(),TopListAlbumsActivity.this.getClass().getName(),"DEBUG",null,"RadioRanksById request");
            }else {
                Log.e("jgj","inter == null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*End by menglingqiang*/
        radioModel.RadioRanksById((int) RankId, local_page).doOnSubscribe(new Action0() {
            @Override
            public void call() {
//                showProgress();
                if(is_refresh){
                    progress_circular.setVisibility(View.VISIBLE);
                }
            }
        }).subscribe(new CommonSubscriber<TopListAlbumBean>() {
            @Override
            public void onSuccess(TopListAlbumBean response) {
//                hideProgress();
                progress_circular.setVisibility(View.INVISIBLE);
                swipe_refrsh_ranks.finishRefresh();
                swipe_refrsh_ranks.finishLoadmore();
                if(response.getData()==null){
                    swipe_refrsh_ranks.setEnableLoadmore(false);
                    return;
                }
                if(page_index>response.getData().getTotalPage()){
                    response.getData().setPageData(new ArrayList<>());
                }
                if (response.getData().getPageData() != null&&response.getData().getPageData()!=null&&response.getData().getPageData().size()>0) {
                    if (is_refresh) {
                        datas.clear();
                        page_index = 1;
                    }
                    tv_show_empty.setVisibility(View.GONE);
                    datas.addAll(response.getData().getPageData());
                    adapter.setData(datas);
                    adapter.notifyDataSetChanged();
                    page_index++;
//                    tv_show_empty.setVisibility(View.GONE);
                } else {
                    swipe_refrsh_ranks.setEnableLoadmore(false);
                    if(is_refresh){
                        tv_show_empty.setVisibility(View.VISIBLE);
                    }
                }
                /*Start by menglingqiang*/
                Date dt = new Date();
                try {
                    TracelogInterface inter = MusicApplication.getLogInterface();
                    if(inter != null){
                        inter.sendLogData(dt.toString(),dt.toString(),TopListAlbumsActivity.this.getClass().getName(),"DEBUG",response.getStatusCode().toString(),"RadioRanksById onSuccess");
                    }else {
                        Log.e("jgj","inter == null");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                /*End by menglingqiang*/
            }

            @Override
            public void onFailure(BaseBean response) {
                ToastUtils.show(response.getStatusMessage());
                swipe_refrsh_ranks.finishRefresh();
//                hideProgress();
                progress_circular.setVisibility(View.INVISIBLE);
                if(is_refresh){
                    tv_show_empty.setVisibility(View.VISIBLE);
                }
                /*Start by menglingqiang*/
                Date dt = new Date();
                try {
                    TracelogInterface inter = MusicApplication.getLogInterface();
                    if(inter != null){
                        inter.sendLogData(dt.toString(),dt.toString(),TopListAlbumsActivity.this.getClass().getName(),"ERROR",response.getStatusCode().toString(),"RadioRanksById onFailure");
                    }else {
                        Log.e("jgj","inter == null");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                /*End by menglingqiang*/
            }
        });
    }
}
